﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Logic;
using Task.Common.Entity;
using System.Web.UI.HtmlControls;
using Task.Common.Interface;
using Common.Service;
using Common.BasePages;
using OC.Common.Entity;

namespace Task
{
    public partial class Trust : BasePage
    {
        public string TabIndex;
        private IDelegationTask _IDelegationTask;
        private IDelegation _Delegation;
        private OC.Common.Entity.UserEntity _User;
        public OC.Common.Entity.UserEntity User
        {
            get
            {
                if (_User == null)
                {
                    _User = (OC.Common.Entity.UserEntity)UserInfo;
                }
                return _User;
            }
        }
        public IDelegationTask IDelegationTask
        {
            get
            {
                if (_IDelegationTask == null)
                {
                    _IDelegationTask = ServiceContainer.GetService<IDelegationTask>();
                }
                return _IDelegationTask;
            }
        }

        public IDelegation IDelegation
        {
            get
            {
                if (_Delegation == null)
                {
                    _Delegation = ServiceContainer.GetService<IDelegation>();
                }
                return _Delegation;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TabIndex = "0";
                BindData(0);
                BindTrustList(0);
                BindTrustTaskList(0);
            }
        }
        #region 委托事项设置
        public void BindData(int index)
        {
            int pageSize = 10;
            int count = 0;
            PagedDataSource page = new PagedDataSource();
            string whereStr = " where 1=1 and  trim(TASK_USER)='" + User.USER_CODE.Trim() + "'";
            if (!string.IsNullOrEmpty(ddlProcessCatalog.SelectedValue)&& ddlProcessCatalog.SelectedValue!="-1")
            {
                whereStr += " and PROCESS_NAME='" + ddlProcessCatalog.SelectedItem.Text + "'";
            }
            if (!String.IsNullOrEmpty(txtBeginTime.Value) && !String.IsNullOrEmpty(txtBeginTime2.Value))
            {
                whereStr += " and START_DATE between to_date('" + txtBeginTime.Value + "','yyyy-MM-dd') and to_date('" + txtBeginTime2.Value + "','yyyy-MM-dd') ";
            }
            if (!String.IsNullOrEmpty(txtEndDate.Value) && !String.IsNullOrEmpty(txtEndDate2.Value))
            {
                whereStr += " and EFFECT_FROM between to_date('" + txtEndDate.Value + "','yyyy-MM-dd') and to_date('" + txtEndDate2.Value + "','yyyy-MM-dd') ";
            }
            if (!string.IsNullOrEmpty(txtUser.Value))
            {
                whereStr += " and TASK_USER = '" + txtUser.Value + "'";
            }
            if (!string.IsNullOrEmpty(ddlStatus.SelectedValue) && ddlStatus.SelectedValue != "-1")
            {
                whereStr += " and DELEGATOR_STATUS='" + ddlStatus.SelectedValue + "'";
            }
            List<DelegationEntitiy> lists = new DelegationLogic().GetList(whereStr);
            int totalCount = lists.Count;
            page.DataSource = lists;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rptList.DataSource = page;
            rptList.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();

        }
        #endregion
        #region 被委托事项列表
        public void BindTrustList(int index)
        {
            int pageSize = 10;
            int count = 0;
            PagedDataSource page = new PagedDataSource();

            string whereStr = " where 1=1 ";
            if (!string.IsNullOrEmpty(ddlTrustStatus.SelectedValue) && ddlProcessCatalog.SelectedValue != "-1")
            {
                whereStr += " and PROCESS_NAME='" + ddlTrustStatus.SelectedItem.Text + "'";
            }
            if (!String.IsNullOrEmpty(txtStartDate.Value) && !String.IsNullOrEmpty(txtStartDate2.Value))
            {
                whereStr += " and START_DATE between to_date('" + txtStartDate.Value + "','yyyy-MM-dd') and to_date('" + txtStartDate2.Value + "','yyyy-MM-dd') ";
            }
            if (!String.IsNullOrEmpty(txtFromDate.Value) && !String.IsNullOrEmpty(txtFromDate2.Value))
            {
                whereStr += " and EFFECT_FROM between to_date('" + txtFromDate.Value + "','yyyy-MM-dd') and to_date('" + txtFromDate2.Value + "','yyyy-MM-dd') ";
            }
            if (!string.IsNullOrEmpty(txtTrustUser.Value))
            {
                whereStr += " and TASK_USER = '" + txtTrustUser.Value + "'";
            }
            if (!string.IsNullOrEmpty(ddlTrustStatus.SelectedValue) && ddlTrustStatus.SelectedValue != "-1")
            {
                whereStr += " and DELEGATOR_STATUS='" + ddlTrustStatus.SelectedValue + "'";
            }
            whereStr += " and  trim(delegator_user)='" + User.USER_CODE.Trim() + "'";
            List<DelegationEntitiy> lists = new DelegationLogic().GetList(whereStr);
            int totalCount = lists.Count;
            page.DataSource = lists;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rptTrustList.DataSource = page;
            rptTrustList.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();

        }
        #endregion
        #region 委托任务列表
        public void BindTrustTaskList(int index)
        {
            int pageSize = 10;
            int count = 0;
            PagedDataSource page = new PagedDataSource();

            string whereStr = " where 1=1";
            if (!string.IsNullOrEmpty(ddlTrustTaskProcess.SelectedValue) && ddlProcessCatalog.SelectedValue != "-1")
            {
                whereStr += " and PROCESS_NAME='" + ddlTrustTaskProcess.SelectedItem.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtTitle.Value))
            {
                whereStr += " and TASK_TITLE like '%" + txtTitle.Value + "%'";
            }
            if (!string.IsNullOrEmpty(txtNum.Value))
            {
                whereStr += " and SUMMARY like '%" + txtNum.Value + "%'";
            }
            if (!string.IsNullOrEmpty(txtDate.Value) && !String.IsNullOrEmpty(txtDate2.Value))
            {
                whereStr += " and START_TIME between to_date('" + txtDate.Value + "','yyyy-MM-dd') and to_date('" + txtDate2.Value + "','yyyy-MM-dd') ";
            }
            if (!string.IsNullOrEmpty(txtUserName.Value))
            {
                whereStr += " and RECIPIENT = '" + txtUserName.Value + "'";
            }
            if (!string.IsNullOrEmpty(ddlTrustTaskProcess.SelectedValue) && ddlStatus.SelectedValue != "-1")
            {
                whereStr += " and STATUS='" + ddlTrustTaskProcess.SelectedValue + "'";
            }
            List<DelegationTaskEntity> lists = IDelegationTask.GetList(whereStr);
            int totalCount = lists.Count;
            page.DataSource = lists;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rptTrustTaskList.DataSource = page;
            rptTrustTaskList.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();

        }
        #endregion
        /// <summary>
        /// 获取委托状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public string GetStatusString(string status)
        {
            string str = "";
            switch (status)
            {
                case "0":
                    str = "待确认";
                    break;
                case "1":
                    str = "已完成";
                    break;
                case "2":
                    str = "已取消";
                    break;
                case "3":
                    str = "已确认生效";
                    break;
                case "4":
                    str = "已拒绝";
                    break;
                default:
                    break;
            }
            return str;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            TabIndex = "0";
            BindData(0);
        }

        protected void btnSelectTrust_Click(object sender, EventArgs e)
        {
            TabIndex = "1";
            BindTrustList(0);
        }

        protected void btnSelectTrustTaskList_Click(object sender, EventArgs e)
        {
            TabIndex = "2";
            BindTrustTaskList(0);
        }

        protected void pagination_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            BindData(e.NewPageIndex - 1);
        }

        protected void pagination1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            BindTrustList(e.NewPageIndex - 1);
        }

        protected void pagination2_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            BindTrustTaskList(e.NewPageIndex - 1);
        }
        /// <summary>
        /// 取消委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            TabIndex = "0";
            string strId = string.Empty;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox checkbox = ((HtmlInputCheckBox)item.FindControl("cboList"));
                if (checkbox.Checked)
                {
                    strId += ((HiddenField)item.FindControl("hidListID")).Value + ",";
                }
            }
            if (strId.Length > 0)
            {
                strId = strId.Substring(0, strId.Length - 1);
            }
            else
            {
                Page.RegisterClientScriptBlock("ok", "<script>alert('请选择要操作的数据！')</script>");
            }
            int count = IDelegation.UpdateData(strId, 2);
            if (count > 0)
            {
                Page.RegisterClientScriptBlock("ok", "<script>alert('取消委托成功！')</script>");
            }
            else
            {
                Page.RegisterClientScriptBlock("fail", "<script>alert('取消委托失败！')</script>");
            }
        }
        /// <summary>
        /// 确认委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAffirm_Click(object sender, EventArgs e)
        {
            TabIndex = "1";
            string strId = string.Empty;
            foreach (RepeaterItem item in rptTrustList.Items)
            {
                HtmlInputCheckBox checkbox = ((HtmlInputCheckBox)item.FindControl("cboTrustList"));
                if (checkbox.Checked)
                {
                    strId += ((HiddenField)item.FindControl("hidTrustId")).Value + ",";
                }
            }
            if (strId.Length > 0)
            {
                strId = strId.Substring(0, strId.Length - 1);
            }
            else
            {
                Page.RegisterClientScriptBlock("ok", "<script>alert('请选择要操作的数据！')</script>");
            }
            int count = IDelegation.UpdateData(strId, 3);
            if (count > 0)
            {
                Page.RegisterClientScriptBlock("ok", "<script>alert('确认委托成功！')</script>");
            }
            else
            {
                Page.RegisterClientScriptBlock("fail", "<script>alert('确认委托失败！')</script>");
            }
        }
        /// <summary>
        /// 拒绝委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRefuse_Click(object sender, EventArgs e)
        {
            TabIndex = "1";
            string strId = string.Empty;
            foreach (RepeaterItem item in rptTrustList.Items)
            {
                HtmlInputCheckBox checkbox = ((HtmlInputCheckBox)item.FindControl("cboTrustList"));
                if (checkbox.Checked)
                {
                    strId += ((HiddenField)item.FindControl("hidTrustId")).Value + ",";
                }
            }
            if (strId.Length > 0)
            {
                strId = strId.Substring(0, strId.Length - 1);
            }
            else
            {
                Page.RegisterClientScriptBlock("ok", "<script>alert('请选择要操作的数据！')</script>");
            }
            int count = IDelegation.UpdateData(strId, 4);
            if (count > 0)
            {
                Page.RegisterClientScriptBlock("ok", "<script>alert('拒绝委托成功！')</script>");
            }
            else
            {
                Page.RegisterClientScriptBlock("fail", "<script>alert('拒绝委托失败！')</script>");
            }
        }
    }
}